/**
 * Copyright (C) 2006, 2007 geolabs
 * author: Gérald Fenoy gerald [ at ] geolabs [dot] fr
 *
 * Ajax static class
 *
 */
Ajax=Class.create({
  request: function(){      
      var request = null;
      if(typeof XMLHttpRequest != "undefined") {
	try {
	  request = new XMLHttpRequest();
	} catch(e) {
	  request = false;
	  throw("[octometa: Ajax.js error]\n"+e);
	}
      } 
      else if(window.ActiveXObject) {
	try {
	  request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
	  try {
	    request = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch(e) {
	    request = false;
	    throw("[octometa: Ajax.js error]\n"+e);
	  }
	}
      }
      return request;
    },

  send: function(request,url,options){
      request.open((options.method?options.method:"GET"), url, (options.async?options.async:true));
      if(options.method=='POST')
	request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      request.send((options.method!='POST'?null:options.args));
    },

  parseArg: function(argv){
      var tmp="";

      /**
       * Method instanceOf works only on firefox 
       if(instanceOf(argv,Object))
       for(var i in argv)
       tmp+=i+"="+argv[i]+'&';
       else
       if(instanceOf(argv,Array))
       for(var i=0;i<argv.length;i++)
       tmp+=argv[i][0]+'='+argv[i][1]+'&';
      */
      
      for(var i in argv){
	if(argv[i] && argv[i].replace)
	  argv[i]=argv[i].replace(/&/g,"%26").replace(/=/g,"%3D");
	tmp+=i+"="+argv[i]+'&';
      }

      var timestamp=new Date();
      tmp+="timestamp="+(timestamp.toUTCString());
      return tmp;
    }
  });
